﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication2.Controllers
{
    /// <summary>
    /// Classe estesione controller
    /// </summary>
    public class ControllerEx : Controller
    {
        /// <summary>
        /// Qui prendo parametro lang da url per localizzare pagina
        /// </summary>
        //protected override void OnActionExecuting(ActionExecutingContext filterContext)
        //{
        //    try
        //    {
        //        if (!RouteData.Values.ContainsKey("lang"))
        //        {
        //            var userLanguages = this.Request.UserLanguages;
        //            if (userLanguages != null)
        //            {
        //                var lang = userLanguages[0];
        //                var lang2 = lang.Substring(0, 2);
        //                var actionDescriptor = filterContext.ActionDescriptor;
        //                var actionName = actionDescriptor.ActionName;
        //                var controllerName = actionDescriptor.ControllerDescriptor.ControllerName;

        //                filterContext.Result =
        //                    new RedirectToRouteResult(new RouteValueDictionary
        //                                                  {
        //                                                      {"controller", controllerName},
        //                                                      {"action", actionName},
        //                                                      {"lang", lang2}
        //                                                  });

        //                return;
        //            }


        //        }
        //        else
        //        {
        //            var lang = RouteData.Values["lang"] as string;

        //            if (lang != null)
        //            {
        //                var ci = new CultureInfo(lang);
        //                //Checking first if there is no value in session 
        //                //and set default language 
        //                //this can happen for first user's request

        //                //Finally setting culture for each request
        //                Thread.CurrentThread.CurrentUICulture = ci;
        //                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {

        //    }

        //    base.OnActionExecuting(filterContext);

        //}
        public const string SuccessMessages = "success-messages";

        public const string ErrorMessages = "error-messages";

        protected void AddErrorMessage(string message)
        {
            AddMessage(message, ErrorMessages);
        }

        protected void AddSuccessMessage(string message)
        {
            AddMessage(message, SuccessMessages);
        }

        private void AddMessage(string message, string type)
        {
            try
            {
                List<string> messages = null;

                if (TempData.ContainsKey(type) && TempData[type] is List<string>)
                {
                    messages = TempData[type] as List<string>;

                    if (messages != null)
                    {
                        messages.Add(message);

                        TempData[type] = messages;
                    }
                }
                else
                {
                    messages = new List<string>();
                }

                if (messages != null) messages.Add(message);

                TempData[type] = messages;
            }
            catch (Exception)
            {

                Debug.Fail("Error on add success message");
            }
        }
    }
}